$(function () {
    var operating = null;
    var tip = function (obj, info) {
        obj.tip({
            content: info,
            background: "#000",
            color: "#fff"
        });
    };

    var errorHandler = function (error, obj) {
        if (typeof error.responseJSON == 'undefined') {
            tip(obj, lang('tip.system'));
            return;
        }

        tip(obj, error.responseJSON.info);
    };


    // 记一笔项目设置
    var submitBooks = $('#submitBooks');
    $('.content-panel-project').on('click','.project-list',function(){
        var incomeProject = $('#doc-center .income-project');
        var expendProject = $('#doc-center .expend-project');
        $me = $(this);
        var value = $me.data('value');
        $me.addClass('active').siblings().removeClass('active');
        submitBooks.data('value',value);
        if(value == 1){
            expendProject.addClass('hidden');
            incomeProject.removeClass('hidden');
        }else{
            incomeProject.addClass('hidden');
            expendProject.removeClass('hidden');
        }
    });    
    submitBooks.on('click', function () {
        $me = $(this);
        var type = $me.data('value');
        var name = $('#addBooks').val().trim(),
            reg = window.commonRegexp.nameLength15;
        if (!reg.test(name)) {
            $(this).tip({
                content: lang('hotel.project'),
                background: "#000",
                color: "#fff"
            });
            return false;
        }        
        var param = {
                hotel: window.currentHotel,
                name: name,
                type:type
            };        
        ajaxRequest('/Home/Hotel/addNote', param, 'post', function (data) {
            var info = data.data;
            var projectTemplate = $($('#templateProject').clone().html());
            var projectHTMLs = '';
            if(type == 1){
                projectTemplate.addClass('income-project');
            }else{
                projectTemplate.addClass('expend-project');
            }
            projectTemplate.attr('data-value',info.noteId);
            projectTemplate.find('.project-name').text(info.noteName);
            projectHTMLs += projectTemplate.prop('outerHTML');
            $me.closest('.tag-item').before(projectHTMLs);
            $('#addBooks').val('');
        }, errorHandler, $me);
    });
    // 删除记一笔项目
    $('#doc-center').on('click','.deleteBooksBtn',function (e) {
        noteId = $(this).parents('.tag-item').data('value');
        window.tagItem = $(this).closest('.tag-item');
    });
    // 删除记一笔项目model点击
    $('#submitBooksDelete').on('click', function (e) {
        var param = {
                hotel: window.currentHotel,
                noteId: noteId
            },
            $me = $(this);
        ajaxRequest('/Home/Hotel/deleteNote', param, 'post', function(data){
            window.tagItem.remove();
            $('#deleteBooksModal').modal('hide');
        }, null, $me);
    });
    //修改记一笔项目
    $('#doc-body').on('click','.edit-btn',function(){
        var $me = $(this);
        var editContent = $me.closest('.edit-content');
        var value = editContent.parents('.tag-item').find('.nowrap-content').text();
        $me.addClass('hidden');
        editContent.find('.confirm-btn').removeClass('hidden');
        editContent.find('.edit-input').val(value).removeClass('hidden');
    }).on('click','.confirm-btn',function(){
        var $me = $(this);
        var editContent = $me.closest('.edit-content'); 
        var value = editContent.find('.edit-input').val();
        var param = {
                hotel: window.currentHotel,
                name: editContent.find('.edit-input').val(),
                noteId:editContent.parents('.tag-item').data('value')
        };   
        ajaxRequest('/Home/Hotel/editNote', param, 'post', function (data) {
            editContent.parents('.tag-item').find('.nowrap-content').text(value);
            $me.addClass('hidden');
            editContent.find('.edit-input').val(value).addClass('hidden');
            editContent.find('.edit-btn').removeClass('hidden');
        }, function(error){
            if (error.responseJSON !== undefined) {
                modalAlert(error.responseJSON.info);
            }            
        });
    });        

    // 其他消费项目设置
    $('#submitEspeciallyBooks').on('click', function () {
        var name = $('#addEspeciallyBooks').val().trim(),
            reg = window.commonRegexp.nameLength15;
        if (!reg.test(name)) {
            $(this).tip({
                content: lang('hotel.project'),
                background: "#000",
                color: "#fff"
            });
            return false;
        } 
        var param = {
                hotel: window.currentHotel,
                name: name
            };   
        $me = $(this);
        ajaxRequest('/Home/Hotel/addOtherCost', param, 'post', function (data) {
            window.location.reload();
        }, errorHandler, $me);
    });
    // 删除其他消费项目
    $('.deleteEspecially').on('click', function (e) {
        costId = $(this).parents('.tag-item').data('value');
    });
    // 删除其他消费项目model点击
    $('#submitEspeciallyDelete').on('click', function (e) {
        var param = {
                hotel: window.currentHotel,
                costId: costId
            },
            $me = $(this);
        ajaxRequest('/Home/Hotel/deleteOtherCost', param, 'post', null, null, $me);
    });
    
    //修改其他消费项目
    $('#doc-body').on('click','.editother-btn',function(){
        var $me = $(this);
        var editContent = $me.closest('.edit-content');
        var value = editContent.parents('.tag-item').find('.nowrap-content').text();
        $me.addClass('hidden');
        editContent.find('.confirmother-btn').removeClass('hidden');
        editContent.find('.editother-input').val(value).removeClass('hidden');
    }).on('click','.confirmother-btn',function(){
        var $me = $(this);
        var editContent = $me.closest('.edit-content'); 
        var value = editContent.find('.editother-input').val();
        var param = {
                hotel: window.currentHotel,
                name: editContent.find('.editother-input').val(),
                costId:editContent.parents('.tag-item').data('value')
        };   
        ajaxRequest('/Home/Hotel/editOtherCost', param, 'post', function (data) {
            editContent.parents('.tag-item').find('.nowrap-content').text(value);
            $me.addClass('hidden');
            editContent.find('.editother-input').val(value).addClass('hidden');
            editContent.find('.editother-btn').removeClass('hidden');
        }, function(error){
            if (error.responseJSON !== undefined) {
                modalAlert(error.responseJSON.info);
            }            
        });
    });          

    // 查房消费物品设置
    $('#submitWardRoundBooks').on('click', function () {
        var name = $('#addWardRoundBooks').val().trim(),
            reg = window.commonRegexp.nameLength15;
        if (!reg.test(name)) {
            $(this).tip({
                content: lang('hotel.project'),
                background: "#000",
                color: "#fff"
            });
            return false;
        } 
        var param = {
                hotel: window.currentHotel,
                name: name
            };   
        $me = $(this);
        ajaxRequest('/Home/Hotel/addRoomCost', param, 'post', function (data) {
            window.location.reload();
        }, errorHandler, $me);
    });
    // 删除查房消费物品设置
    $('.deleteWardRound').on('click', function (e) {
        costId = $(this).parents('.tag-item').data('value');
    });
    // 删除查房消费物品设置model点击
    $('#submitWardRoundDelete').on('click', function (e) {
        var param = {
                hotel: window.currentHotel,
                costId: costId
            },
            $me = $(this);
        ajaxRequest('/Home/Hotel/deleteRoomCost', param, 'post', null, null, $me);
    });
    
    //修改查房消费物品设置
    $('#doc-body').on('click','.wardRound-btn',function(){
        var $me = $(this);
        var editContent = $me.closest('.edit-content');
        var value = editContent.parents('.tag-item').find('.nowrap-content').text();
        $me.addClass('hidden');
        editContent.find('.confirmWardRound-btn').removeClass('hidden');
        editContent.find('.wardRound-input').val(value).removeClass('hidden');
    }).on('click','.confirmWardRound-btn',function(){
        var $me = $(this);
        var editContent = $me.closest('.edit-content'); 
        var value = editContent.find('.wardRound-input').val();
        var param = {
                hotel: window.currentHotel,
                name: editContent.find('.wardRound-input').val(),
                costId:editContent.parents('.tag-item').data('value')
        };   
        ajaxRequest('/Home/Hotel/editRoomCost', param, 'post', function (data) {
            editContent.parents('.tag-item').find('.nowrap-content').text(value);
            $me.addClass('hidden');
            editContent.find('.wardRound-input').val(value).addClass('hidden');
            editContent.find('.wardRound-btn').removeClass('hidden');
        }, function(error){
            if (error.responseJSON !== undefined) {
                modalAlert(error.responseJSON.info);
            }            
        });
    });        

    // 添加支付方式
    $('#submitAdd').on('click', function () {
        var name = $('#addName').val().trim(),
            reg = window.commonRegexp.nameLength15;
        if (!reg.test(name)) {
            $(this).tip({
                content: lang('hotel.payStyle'),
                background: "#000",
                color: "#fff"
            });
            return false;
        }

        var param = {
                hotel: window.currentHotel,
                name: name
            },
            $me = $(this);
        ajaxRequest('/Home/Hotel/addPay', param, 'post', function (data) {
            $('#addName').val('');
            window.location.reload();
        }, errorHandler, $me);
    });
    // 删除支付方式
    $('.deleteBtn').on('click', function (e) {
        operating = $(this).parents('.tag-item').data('value');
        if($(this).data('usepay') == 0){
            $('#deleteModal').modal('show');
        }else{
            $('#inoperableModal').modal('show');
        }
    });
    // 删除支付方式model点击
    $('#submitDelete').on('click', function (e) {
        var param = {
                hotel: window.currentHotel,
                pay: operating
            },
            $me = $(this);
        ajaxRequest('/Home/Hotel/deletePay', param, 'post', null, null, $me);
    });


    // 初始化支付短信通知以及支付结算信息
    ajaxRequest('/Home/Hotel/getPayDetail', {
        'hotel': window.currentHotel
    }, 'post', function (data) {
        var hotelInfo = data.data;
        if (hotelInfo.orderpaymobile !== '') {
            $('#payMobileTag').removeClass('hidden');
            $('#payMobile').html('+'+hotelInfo.orderpayareacode +'&nbsp;'+ hotelInfo.orderpaymobile);
        }
        if (hotelInfo.accounttype === '1') {
            $('#info-form').find('[name="requestAccountType"][value="0"]').parents('.radio')[0].MaterialRadio.uncheck();
            $('#info-form').find('[name="requestAccountType"][value="1"]').parents('.radio')[0].MaterialRadio.check();
        }
        $('#requestBank').val(hotelInfo.bankname);
        $('#requestName').val(hotelInfo.accountname);
        $('#requestAccount').val(hotelInfo.accountnumber);
        $('#confirmAccount').val(hotelInfo.accountnumber);
        $('#info-form .areaCodeValue').text(hotelInfo.accountareacode);
        $('#requestNotificationMobile').val(hotelInfo.accountmobile);

    }, function (error) {});

    $('#payMobileDeleteBtn').on('click', function () {
        var param = {
                hotel: window.currentHotel
            },
            submitBtn = $(this).parents('.tag-item-inner');
        ajaxRequest('/Home/Hotel/deletePayMobile', param, 'post', function (data) {
            $('#payMobileTag').addClass('hidden');
        }, errorHandler, submitBtn);
    });

    // 添加支付短信通知
    $('#submitAddMobile').on('click', function () {
        var mobile = $('#addMobile').val().trim(),
            areaCode = $(this).closest('.content-panel-body').find('.areaCodeValue').text().trim(),
            reg = window.commonRegexp.mobile;
        if (!reg.test(mobile)) {
            tip($(this), lang('tip.mobile'));
            return false;
        }

        var param = {
                hotel: window.currentHotel,
                mobile: mobile,
                areaCode : areaCode
            },
            submitBtn = $(this);
        ajaxRequest('/Home/Hotel/addPayMobile', param, 'post', function (data) {
            $('#addMobile').val('');
            $('#payMobileTag').removeClass('hidden');
            $('#payMobile').html(mobile);
        }, errorHandler, submitBtn);
    });

    $('#info-form').inputValidate({
        submit: {
            query: '#submitInfo'
        },
        inputs: [{
            query: '#requestBank',
            regexp: window.commonRegexp.nameLength20,
            message: lang('requestBank'),
            emptyMessage: lang('requestBankNull')
        }, {
            query: '#requestName',
            regexp: window.commonRegexp.nameLength20,
            message: lang('requestName'),
            emptyMessage: lang('requestNameNull')
        }, {
            query: '#requestAccount',
            regexp: window.commonRegexp.bankNumber,
            message: lang('requestAccount'),
            emptyMessage: lang('requestAccountNull')
        }, {
            query: '#confirmAccount',
            regexp: window.commonRegexp.bankNumber,
            message: lang('requestAccount'),
            emptyMessage: lang('confirmAccountNull')
        }, {
            query: '#requestNotificationMobile',
            regexp: window.commonRegexp.mobile,
            message: lang('book.mobile'),
            emptyMessage: lang('book.mobileNull')
        }]
    });
    $('#submitInfo').on('successSubmit.amos.inputValidate', function (e) {
        var submitBtn = $(this);
        //检查两次输入银行卡是否一致
        if ($('#requestAccount').val() != $('#confirmAccount').val()) {
            formInlineTip(submitBtn, lang('TwoBank'));
            return false;
        }
        var params = {};
        params.hotel = window.currentHotel;
        params.type = $('#requestAccountType').prop('checked') ? '0' : '1';
        params.name = $('#requestName').val();
        params.bank = $('#requestBank').val();
        params.account = $('#requestAccount').val();
        params.mobile = $('#requestNotificationMobile').val();
        params.areaCode = $('#info-form .areaCodeValue').text().trim(),
        formInlineRight(submitBtn);
        ajaxRequest('/Home/Hotel/savePayDetail', params, 'post', function (data) {
            formInlineTip(submitBtn, lang('successfullySave'), 2);
        }, null, submitBtn);

    });
    //应用程序签发打印开关
    var wardRoundSwitch = $('#wardRoundSwitch');
    var printSwitch = $('#printSwitch');
    ajaxRequest('/Home/WindowsClientSetting/getStatus', {
        'hotel': window.currentHotel
    }, 'post', function (data) {
        var status = data.data.status;
        var checkRoom = data.data.checkRoom;
        if(status){
            printSwitch.addClass('is-checked');
        }else{
            printSwitch.removeClass('is-checked');
        }        
        if(checkRoom){
            wardRoundSwitch.addClass('is-checked');
        }else{
            wardRoundSwitch.removeClass('is-checked');
        }
    }, function (error) {
        formInlineTip(printSwitch, error.responseJSON.info, 0);
    });
    
    printSwitch.click(function(){
        var submitBtn = $(this);
        if(submitBtn.hasClass('is-checked')){
           var status = 0;
        }else{
           var status = 1;
        }    
        var params = {};
        params.hotel = window.currentHotel;
        params.status = status;
        formInlineRight(submitBtn);
        ajaxRequest('/Home/WindowsClientSetting/setStatus', params, 'post', function (data) {
            if(status == 0){
               submitBtn.removeClass('is-checked');
            }else{
               submitBtn.addClass('is-checked');
            }   
            formInlineTip(submitBtn, lang('successfullySave'), 2);
        }, null, submitBtn);       
    }); 
    //查房开关
    wardRoundSwitch.click(function(){
        var submitBtn = $(this);
        if(submitBtn.hasClass('is-checked')){
           var type = 0;
        }else{
           var type = 1;
        }    
        var params = {};
        params.hotel = window.currentHotel;
        params.type = type;
        formInlineRight(submitBtn);
        ajaxRequest('/Home/Hotel/toggleRoomCheck', params, 'post', function (data) {
            if(type == 0){
               submitBtn.removeClass('is-checked');
            }else{
               submitBtn.addClass('is-checked');
            }   
            formInlineTip(submitBtn, lang('successfullySave'), 2);
        }, null, submitBtn);       
    });      
});
